package org.alfresco.repo.publishing.hello;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.publishing.AbstractChannelType;
import org.alfresco.repo.publishing.PublishingModel;
import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

public class HelloChannelType extends AbstractChannelType {

	private final static Log log = LogFactory.getLog(HelloChannelType.class);
	public final static String ID = "hello";
	public final static QName HELLO_CHANNEL = QName.createQName(
			"http://www.alfresco.org/model/publishing/hello/1.0",
			"DeliveryChannel");
	public final static QName HELLO_CHANNEL_ASSET = QName.createQName(
			"http://www.alfresco.org/model/publishing/hello/1.0",
			"AssetAspect");
	private final static Set<String> DEFAULT_SUPPORTED_MIME_TYPES = CollectionUtils
			.unmodifiableSet(MimetypeMap.MIMETYPE_VIDEO_MPG,
					MimetypeMap.MIMETYPE_VIDEO_MP4,
					MimetypeMap.MIMETYPE_VIDEO_FLV,
					MimetypeMap.MIMETYPE_VIDEO_3GP,
					MimetypeMap.MIMETYPE_VIDEO_AVI,
					MimetypeMap.MIMETYPE_VIDEO_QUICKTIME,
					MimetypeMap.MIMETYPE_VIDEO_WMV, MimetypeMap.MIMETYPE_HTML,
					MimetypeMap.MIMETYPE_XHTML, MimetypeMap.MIMETYPE_WORD,
					MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_PDF);
	
	private NodeService nodeService;

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

	public boolean canPublish() {
		return true;
	}

	public boolean canPublishStatusUpdates() {
		return true;
	}

	public boolean canUnpublish() {

		return true;
	}

	public QName getChannelNodeType() {

		return HELLO_CHANNEL;
	}

	public String getId() {
		return ID;
	}

	@Override
	public void sendStatusUpdate(Channel channel, String status) {
		log.debug(String.format("updating status on hello world channel: %s",
				status));
	}

	@Override
	public Set<String> getSupportedMimeTypes() {
		return DEFAULT_SUPPORTED_MIME_TYPES;
	}

	@Override
	public void publish(NodeRef nodeToPublish,
			Map<QName, Serializable> channelProperties) {
		log.debug(String
				.format("publishing node %s with properties [%s]",
						nodeToPublish.getId(),
						buildStringProperties(channelProperties)));
		nodeService.addAspect(nodeToPublish, HELLO_CHANNEL_ASSET, null);
        nodeService.setProperty(nodeToPublish, PublishingModel.PROP_ASSET_ID, "some id");
        nodeService.setProperty(nodeToPublish, PublishingModel.PROP_ASSET_URL, "http://heloworld.com/someurl");
	}

	private String buildStringProperties(
			Map<QName, Serializable> channelProperties) {
		String value = "";
		for (Entry<QName, Serializable> entry : channelProperties.entrySet()) {
			value += String.format("%s=%s,", entry.getKey().getNamespaceURI(),
					entry.getValue());
		}
		return value;
	}

	@Override
	public void unpublish(NodeRef nodeToUnpublish,
			Map<QName, Serializable> channelProperties) {
		log.debug(String.format("unpublishing node %s with properties [%s]",
				nodeToUnpublish.getId(),
				buildStringProperties(channelProperties)));
		nodeService.removeAspect(nodeToUnpublish, HELLO_CHANNEL_ASSET);
	}
	
	@Override
	public String getNodeUrl(NodeRef node) {
		return (String) nodeService.getProperty(node, PublishingModel.PROP_ASSET_URL);
	}
	
	@Override
	public Resource getIcon(String sizeSuffix) {
		return new UrlResource( getClass().getResource("icon.jpg") );
	}
	
	@Override
	public String getIconFileExtension() {
		return "jpg";
	}

}
